package com.tyun.common.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.tyun.modules.sys.properties.Pro;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.Date;

/**
 * 关于文件路径的处理
 *
 * @author taocongcong
 * @create 2017-09-04 11:04
 */
public class FilePathUtil {

    private final static Pro pro = (Pro) SpringContextUtils.getBean("pro");

    /**
     * 根据文件名生成一个新的路径
     */
    public static String getFilePath(String sourceFileName) {
        String baseFolder = pro.getBaseUploadURL() + File.separator + "images";
        Date nowDate = new Date();
        // yyyy/MM/dd
        String fileFolder = baseFolder + File.separator + DateUtil.format(nowDate, "yyyy")
                + File.separator + DateUtil.format(nowDate, "MM") + File.separator
                + DateUtil.format(nowDate, "dd");
        File file = new File(fileFolder);
        if (!file.isDirectory()) {
            // 如果目录不存在，则创建目录
            file.mkdirs();
        }
        // 生成新的文件名
        String fileName = DateUtil.format(nowDate, "yyyyMMddhhmmssSSSS")
                + RandomUtil.randomInt(100, 9999) + "." + StringUtils.substringAfterLast(sourceFileName, ".");
        return fileFolder + File.separator + fileName;
    }

    /**
     * 生成一个二维码路径
     */
    public static String setFilePath() {
        String baseFolder =  pro.getBaseUploadURL() + File.separator + "images";
        Date nowDate = new Date();
        // yyyy/MM/dd
        String fileFolder = baseFolder + File.separator + DateUtil.format(nowDate, "yyyy")
                + File.separator + DateUtil.format(nowDate, "MM") + File.separator
                + DateUtil.format(nowDate, "dd");
        File file = new File(fileFolder);
        if (!file.isDirectory()) {
            // 如果目录不存在，则创建目录
            file.mkdirs();
        }
        // 生成新的文件名
        String fileName = DateUtil.format(nowDate, "yyyyMMddhhmmssSSSS")
                + RandomUtil.randomInt(100, 9999) + "." + "png";
        return fileFolder + File.separator + fileName;
    }

}
